package com.socio.resources;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.socio.behaviors.DAOBehavior;
import com.socio.behaviors.UserFacebookIDBehavior;
import com.socio.behaviors.UserUDIDBehavior;
import com.socio.model.ClientUser;
import com.socio.model.User;
import com.socio.model.responses.AllTimeFacebookTopTenResponse;
import com.socio.model.responses.AllTimeLeaderboardResponse;

@Path("/allTime")
public class AllTimeResource {

	private static final Gson gson = new Gson();
	
	@SuppressWarnings("unchecked")
	@Path("/leaderboard")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
	public String getLeaderboard(@QueryParam("udid") String udid) {
	
		// set up behaviors
        DAOBehavior userDAO = new UserUDIDBehavior();
        AllTimeLeaderboardResponse response = new AllTimeLeaderboardResponse(false);
		
        // get 3 users with highest quickScores
        String ordering = "quickScore desc";
        Long[] queryRange = new Long[]{0L,3L};
        Object result = userDAO.execute(null, null, ordering, queryRange, null, null);
        List<User> dbUsers = (List<User>)result;
        if(dbUsers.isEmpty() || dbUsers.get(0) == null) {
        	response.setSuccess(true);
        	return gson.toJson(response);
        }
        
        // check to see if days contain user with udid (if specified)
        List<ClientUser> resultingUsers = new ArrayList<ClientUser>();
        boolean containsRequester = false;
        if(udid == null || udid.isEmpty())
        	containsRequester = true;
        for(User user : dbUsers) {
        	ClientUser resultingUser = user.getClientUser();
        	if(!containsRequester && user.getAndroidUDID().equalsIgnoreCase(udid)) {
        		resultingUser.setNickname("You");
        		if(!user.getFacebookID().isEmpty())
        			resultingUser.setScoring(true);
        		containsRequester = true;
        	}
        	
        	resultingUsers.add(resultingUser);
        }
        if(containsRequester) {
        	response.setSuccess(true);
        	response.setUsers(resultingUsers);
        	return gson.toJson(response);
        }
        
        // if user not part of top 3 get them and add them as a fourth
    	result = userDAO.read(udid, null);
        List<User> dbUser = (List<User>)result;
        if(!dbUser.isEmpty() && dbUser.get(0) != null) {
        	User user = (User)dbUser.get(0);
        	ClientUser requestingUser = user.getClientUser();
        	requestingUser.setNickname("You");
        	
        	// check to see if user is somehow earning points
        	if(!user.getFacebookID().isEmpty())
        		requestingUser.setScoring(true);
            resultingUsers.add(requestingUser);
        }
		
        response.setSuccess(true);
    	response.setUsers(resultingUsers);
    	return gson.toJson(response);
	}
	
	@SuppressWarnings("unchecked")
	@Path("/facebook/top10")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTopTenFacebookUsers() {
		
		// setup
		AllTimeFacebookTopTenResponse response = new AllTimeFacebookTopTenResponse(false);
        DAOBehavior userDAO = new UserFacebookIDBehavior();
        
        // execute custom query
        String ordering = "quickScore desc";
        Long[] queryRange = new Long[]{0L,10L};
        Object result = userDAO.execute(null, null, ordering, queryRange, null, null);
    	List<User> dbUsers = (List<User>)result;
    	List<ClientUser> topFacebookUsers = new ArrayList<ClientUser>();
    	
    	if(dbUsers != null)
    		for(User dbUser : dbUsers)
    			topFacebookUsers.add(dbUser.getClientUser());
    	
        response.setFacebookUsers(topFacebookUsers);
        response.setSuccess(true);
        
        return gson.toJson(response);
	}
}
